package com.pro.wallet.manager;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;


import com.hashkey.wxbasemodule.IPageRouter;

import java.util.HashMap;
import java.util.Map;

public class PageRouterManager implements IPageRouter {
    public final static Map<String, String> pageName = new HashMap<String, String>() {{
        put("first", "first");
        put("second", "second");
        put("tab", "tab");
        put("sample://flutterPage", "flutterPage");
    }};

    public static final String NATIVE_PAGE_URL = "pro://wallet/native/main";
    public static final String FLUTTER_PAGE_URL = "sample://flutterPage";
    public static final String FLUTTER_FRAGMENT_PAGE_URL = "sample://flutterFragmentPage";

    private PageRouterManager() {}

    private static class SingleHolder {
        static IPageRouter instance = new PageRouterManager();
    }

    public static IPageRouter getInstance() {
        return SingleHolder.instance;
    }

    @Override
    public void openPageByUrl(Context context, String url, Map params) {
        openPageByUrl(context, url, params, 0);
    }

    private boolean openPageByUrl(Context context, String url, Map params, int requestCode) {

        String path = url.split("\\?")[0];

        Log.i("openPageByUrl",path);

        try {
            if (pageName.containsKey(path)) {
//                Intent intent = MainActivity.withNewEngine().url(pageName.get(path)).params(params)
//                        .backgroundMode(MainActivity.BackgroundMode.opaque).build(context);
//                if(context instanceof Activity){
//                    Activity activity=(Activity)context;
//                    activity.startActivityForResult(intent,requestCode);
//                }else{
//                    context.startActivity(intent);
//                }
                return true;
            } else if (url.startsWith(FLUTTER_FRAGMENT_PAGE_URL)) {
//                context.startActivity(new Intent(context, FlutterFragmentPageActivity.class));
                return true;
            } else if (url.startsWith(NATIVE_PAGE_URL)) {
                Intent nativeIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                context.startActivity(nativeIntent);
                return true;
            }

            return false;

        } catch (Throwable t) {
            return false;
        }
    }
}
